package com.java.jiuyang.commons.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.jiuyang.commons.dao.GaoDeDao;
import com.java.jiuyang.commons.entity.GaoDeEntity;
import com.java.jiuyang.commons.utils.GaoDeMapUtils;
import com.java.jiuyang.commons.utils.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: GaoDeServiceImpl
 * @Description:
 * @Version: v1.0.0
 * @Author: Fu Hao
 * @Date: 2019/10/24 0024 下午 1:56
 * Modification History:
 * Date           Author      Version     Description
 * -------------------------------------------------------------
 * 2019/10/24 0024        Fu Hao      v1.0.0         创建
 */
@Service
public class GaoDeServiceImpl implements GaoDeService {
    //这个是秘钥
    private static final String keyCode = "87d056f6e8cde7af1fac90480914bafc";
    //高德API路径
    private static final String admAddress = "https://restapi.amap.com/v3/config/district";

    private static final Logger log= LoggerFactory.getLogger(GaoDeServiceImpl.class);

    @Autowired
    private GaoDeDao gaoDeDao;

    /*分页测试*/
    @Override
    public PageInfo getMapParams(Integer page, Integer size) {
        page=(page==null?1:page);
        size=(size==null?10:size);
        PageHelper.startPage(page,size);
        return new PageInfo<>(gaoDeDao.getMapParams(page,size));
    }


    /*去数据库查*/
    @Override
    public PageInfo getmap(Integer page, Integer size, GaoDeEntity gaoDeEntity) {
        page=(page==null?1:page);
        size=(size==null?10:size);
        PageHelper.startPage(page,size);
        //1、判断数据库是否有数据
        List<GaoDeEntity>list=gaoDeDao.getmap(gaoDeEntity);
        //2、没有数据去高德API查询
        if (list.isEmpty()&&gaoDeEntity.getName()!=null){
            Map<String, Object> params = new HashMap<>();
            String keyName=gaoDeEntity.getName();
            params.put("key", keyCode);
            params.put("keywords", keyName);
            params.put("subdistrict", 1);
            params.put("extensions", "base");
            //System.out.println(Collections.singletonList(HttpClientUtil.doGet(admAddress, params))+"高德数据");
        //3、查到数据存进数据库

        //4、返回高德查到的数据给前端
            return new PageInfo(Collections.singletonList(HttpClientUtil.doGet(admAddress, params)));
        }
        return new PageInfo<>(gaoDeDao.getmap(gaoDeEntity));
    }

    /**
     * 根据地址获取经纬度
     * @param address
     * @return
     */
    @Override
    public String getCenter(String address) {
        if (!address.isEmpty()){
            return GaoDeMapUtils.getLonLarByAddress(address);
        }
        try {
            throw new Exception();
        } catch (Exception e) {
            System.out.println("地址不为空");
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 根据经纬度获取地址
     * @param longitude
     * @param latidute
     * @return
     */
    @Override
    public String getAddress(double longitude, double latidute) {
       return GaoDeMapUtils.getAddressByLonLat(longitude,latidute);

    }


}
